
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEventEdit();
});

function getInforProductDetail(){
    var maCTSP = ISS.SESSION.getSession('session_maCTSP');

    var product = {};
    product.maCTSP = maCTSP;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getInformationOfProductDetail',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(product),
        success: function (data) {
            var responseContent = data.responseObject;
            loadDataComplete(responseContent);
        }
    });
}

function loadData() {
    getInforProductDetail();

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllProductListInProduct',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllProductListToViewInProduct(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/ad_GetAllColor',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllColorToView(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/ad_GetAllSize',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllSizeToView(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/ad_GetAllMotifInProductDetail',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllMotifToView(responseContent);
        }
    });

}

function loadDataComplete(product){
    ISS.DOM.setVal('#ProductDetail_code', product.maCTSP);
    ISS.DOM.setVal('#ProductDetail_product_hidden', product.sanPham.maSP);
    ISS.DOM.setVal('#ProductDetail_product', product.sanPham.maSP);
    ISS.DOM.setVal('#ProductDetail_size_hidden', product.kichThuoc.maSize);
    ISS.DOM.setVal('#ProductDetail_color_hidden', product.mauSac.maMau);
    ISS.DOM.setVal('#ProductDetail_motif_hidden', product.hoaTiet.maHoaTiet);
    ISS.DOM.setVal('#ProductDetail_price', product.giaSP);
    ISS.DOM.setVal('#ProductDetail_quantity', product.soLuong);

    bindEventUploadImage();
}

function bindEventUploadImage() {
    ISS.DOM.html('#upload_template', '<div id="multipleupload">Upload</div>');
    var maCTSP = ISS.DOM.getVal('#ProductDetail_code');
    var _upload = $("#multipleupload").uploadFile({
        url: ISS.SESSION.getSession('contextPath') + "/service/admin/insertProductPicture",
        autoSubmit:false,
        multiple:true,
        fileName:"myfile",
        formData: {"maCTSP": maCTSP}
    });

    ISS.EVENT.bindEvent('#startUpload', 'click', function () {
        var maCTSP = ISS.DOM.getVal('#ProductDetail_code');
        ISS.BACKEND.request({
            url: ISS.SESSION.getSession('contextPath') + '/service/admin/deletePicture/' + maCTSP,
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                if(data.code == 0) {
                    _upload.startUpload();
//                    alert('C?p nh?t ?nh m?i thành công');
                }
            }
        });
    });

}

function bindEventEdit(){
    ISS.EVENT.bindEvent('#ProductDetail_Edit1', 'click', function () {
        ISS.DOM.removeClass('#boxTrackConfirm', 'hideBox');
    });

    ISS.EVENT.bindEvent('#buttonEditProductDetail_Agree', 'click', function () {
        ISS.DOM.addClass('#boxTrackConfirm', 'hideBox');
        sendInfoEdit();
    });

    ISS.EVENT.bindEvent('#buttonEditProductDetail_Cancel', 'click', function () {
        ISS.DOM.addClass('#boxTrackConfirm', 'hideBox');
    });

    ISS.EVENT.bindEvent('#ProductDetail_Quit1', 'click', function () {
        ISS.DOM.removeClass('#boxTrackConfirmQuit', 'hideBox');
    });

    ISS.EVENT.bindEvent('#buttonQuitSession_Agree', 'click', function () {
        quitSession();
    });

    ISS.EVENT.bindEvent('#buttonQuitSession_Cancel', 'click', function () {
        ISS.DOM.addClass('#boxTrackConfirmQuit', 'hideBox');
    });

    ISS.EVENT.bindEvent('#ProductDetail_Picture1', 'click', function () {
        ISS.DOM.removeClass('#boxDetailProductPicture', 'hideBox');
    });



    ISS.EVENT.bindEvent('#ProductDetailPicture_Cancel1', 'click', function () {
        ISS.DOM.addClass('#boxDetailProductPicture', 'hideBox');
    });

    ISS.EVENT.bindEvent('#buttonUpdateSuccess_Hide', 'click', function () {
        ISS.DOM.addClass('#boxTrackConfirmUpdateSuccess', 'hideBox');
    });
}

function sendInfoEdit() {
    var productDetail = {};
    var product = {}, size = {}, color = {}, motif ={};

    productDetail.maCTSP = ISS.DOM.getVal('#ProductDetail_code');
    productDetail.giaSP = ISS.DOM.getVal('#ProductDetail_price');
    productDetail.soLuong = ISS.DOM.getVal('#ProductDetail_quantity');

    product.maSP = ISS.DOM.getVal('#ProductDetail_product');
    productDetail.sanPham = product;

    size.maSize = ISS.DOM.getVal('#ProductDetail_size');
    productDetail.kichThuoc = size;

    color.maMau = ISS.DOM.getVal('#ProductDetail_color');
    productDetail.mauSac = color;

    motif.maHoaTiet = ISS.DOM.getVal('#ProductDetail_motif');
    productDetail.hoaTiet = motif;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/ad_UpdateProductDetail',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(productDetail),
        success: function (data) {
            var code = data.code;
            if(code == 0){
//                getInforProductDetail();
                ISS.DOM.removeClass('#boxTrackConfirmUpdateSuccess', 'hideBox');
            } else {
                alert('Cập nhật thất bại');
            }

        }
    });
}

function sendInfoEditComplete(product) {
    if(product) {
//        ISS.WINDOW.location('/jsp/admin/ad-product-size-common.jsp');
//        alert('Cập nhật thành công');
        ISS.DOM.removeClass('#boxTrackConfirm', 'hideBox');
    }
    else{
        alert('Cập nhật không thành công');
    }
}

function quitSession(){
    toDetailPage();
}

function toDetailPage() {
    var maSP = ISS.SESSION.getSession('session_maSP');
    var _url = '/service/admin/toProductDetailPage/' + maSP;
    ISS.WINDOW.location(_url);
}


/*function toDetailPage() {
    var _maSP = ISS.DOM.getVal('#ProductCommon_code');
    var _url = '/service/admin/toProductDetailPage/' + _maSP;
    ISS.WINDOW.location(_url);
}*/

function mapAllProductListToViewInProduct(productList) {
    var productList_code = ISS.DOM.getVal('#ProductCommon_listProduct_hidden');
    var n = '';
    var html = '';
    for(var i=0; i<productList.length; i++) {
        if (productList[i].maLoaiDMSP == productList_code){
            n = 'selected="selected"';
        }
        else{
            n = '';
        }
        html += '<option value="' + productList[i].maLoaiDMSP + '" ' + n + '  >' + productList[i].tenLoaiDMSP + '</option>';
    }
    ISS.DOM.html('#ProductCommon_listProduct', html);

}

function mapAllColorToView(color) {
    var productDetail_color = ISS.DOM.getVal('#ProductDetail_color_hidden');
    var n = '';
    var html = '';
    for(var i=0; i<color.length; i++) {
        if (color[i].maMau == productDetail_color){
            n = 'selected="selected"';
        }
        else{
            n = '';
        }
        html += '<option value="' + color[i].maMau + '"'+n+'>' + color[i].tenMau + '</option>';
    }

    ISS.DOM.html('#ProductDetail_color', html);
}

function mapAllSizeToView(size) {
    var productDetail_size = ISS.DOM.getVal('#ProductDetail_size_hidden');
    var n = '';
    var html = '';
    for(var i=0; i<size.length; i++) {
        if (size[i].maSize == productDetail_size){
            n = 'selected="selected"';
        }
        else{
            n = '';
        }
        html += '<option value="' + size[i].maSize + '"'+n+'>' + size[i].tenSize + ' - ' + size[i].loaiDanhMuc.tenLoai + '</option>';
    }

    ISS.DOM.html('#ProductDetail_size', html);
}

function mapAllMotifToView(motif) {
    var productDetail_motif = ISS.DOM.getVal('#ProductDetail_motif_hidden');
    var n = '';
    var html = '';
    for(var i=0; i<motif.length; i++) {
        if (motif[i].maHoaTiet == productDetail_motif){
            n = 'selected="selected"';
        }
        else{
            n = '';
        }
        html += '<option value="' + motif[i].maHoaTiet + '"'+n+'>' + motif[i].tenHoaTiet + '</option>';
    }

    ISS.DOM.html('#ProductDetail_motif', html);
}
